home *** CD-ROM | disk | FTP | other *** search
- // A RARS-car by Oscar Gustafsson, y92oscgu@isy.liu.se
- // Enjoy
-
- #include <fstream.h>
- #include <iostream.h>
- #include <iomanip.h>
- #include <string.h>
- #include <stdlib.h>
- #include <math.h>
- #include "car.h"
-
- const double KURV_FART_KONST = 5.8;
- const double KURV_BROMS = -24.0;
- const double RAK_BROMS = -28.0;
- const double RAK_BROMS_KVOT = .91;
- const double RAK_BROMS_KONST = 10;
- const double BIG_SLIP = 20.0;
- const double STEER_GAIN = .8;
- const double DAMP_GAIN = 2.0;
- const double DELTA_LANE = 1.5;
- const double WANTED_DIST = 10.0;
- const double BIG_RAD = 3.0;
- const double HS_RAD = 500;
-
- // ofstream filut("osccar2c.dat");
-
- double absolut(double x)
- {
- if (x < 0)
- return -x;
- else
- return x;
- }
-
-
- double kurv_fart(double radius)
- {
- double rad;
- if (!radius)
- return 250.0;
- rad = radius < 0 ? -radius : radius;
-
- return (KURV_FART_KONST * sqrt(rad));
- }
-
- double kurv_kaning(double radius)
- {
- if (absolut(radius > 500))
- return 7.0;
- else
- return 5.0;
- }
-
- double krit_avst(double v0, double v1, double a)
- {
- double dv;
- double utv;
- dv = v1 - v0;
- if (dv > 0.0)
- return 0.0;
- utv = (v0 + .5 * dv) * dv / a;
- return utv;
- }
-
- con_vec OscCar2(situation& s)
- {
- int brake = 0;
- const char name[] = "OscCar2";
- static int init_flag = 1, high_speed=0;
- con_vec result;
- double alpha,
- vc;
- static double lane = -10000;
- static double speed_next = 250;
- static double speed_goal = 250;
- double to_end,
- bias,
- width,
- speed_after;
- static double prev_rad = 10000.0, prev_len = 10000.0;
-
- if (init_flag == 1) {
- my_name_is(name);
- init_flag = 0;
- result.alpha = result.vc = 0;
- return result;
- }
- if (stuck(s.backward, s.v, s.vn, s.to_lft, s.to_rgt, &result.alpha, &result.vc)) {
- return result;
- }
- width = s.to_lft + s.to_rgt;
-
- if (lane < -9000)
- lane = s.to_lft;
-
- if(s.nex_len<1.2 && s.nex_len != 0 && ((s.cur_rad == 0) ||
- absolut(s.cur_rad)>HS_RAD) && (absolut(s.after_rad)>HS_RAD ||
- s.after_rad == 0))
- {
- // filut << "Picaboo" << endl;
- speed_next = kurv_fart(absolut(s.nex_rad)+(width-WANTED_DIST)/(1-cos(s.nex_len)));
- // filut << " " << speed_next << endl;
- high_speed = 2;
- }
-
- if(prev_len != s.cur_len || prev_rad != s.cur_rad)
- {
- prev_rad = s.cur_rad;
- prev_len = s.cur_len;
- if(high_speed)
- high_speed--;
- }
-
-
- if (s.cur_rad == 0) {
- bias = 0.0;
- speed_goal = s.v + 50;
- if (s.nex_rad == 0)
- speed_next = 250;
- else {
- if(!high_speed)
- speed_next = kurv_fart(absolut(s.nex_rad) + WANTED_DIST);
- if (s.nex_rad < 0)
- lane = width - WANTED_DIST;
- else
- lane = WANTED_DIST;
- }
- }
- else
- {
- if ((s.nex_rad * s.cur_rad < 0) || (s.nex_rad == 0 && s.after_rad * s.cur_rad < 0)) {
- if (s.to_end < s.cur_len / 2)
- lane = width / 2;
- if (s.to_end < s.cur_len / 6)
- if (s.cur_rad < 0)
- lane = WANTED_DIST;
- else
- lane = width - WANTED_DIST;
- }
- else
- {
- if (s.cur_rad < 0)
- lane = width - WANTED_DIST;
- else
- lane = WANTED_DIST;
- }
-
-
- if(high_speed==1)
- speed_goal = kurv_fart(absolut(s.cur_rad)+width/(1-cos(s.cur_len)));
- else
- {
- if (s.cur_rad < 0)
- speed_goal = kurv_fart(width - lane - s.cur_rad);
- else
- speed_goal = kurv_fart(s.cur_rad + lane);
-
- if (s.cur_len > BIG_RAD && s.to_end < s.cur_len)
- speed_goal = kurv_fart(absolut(s.cur_rad) + width / 2);
- }
-
- bias = (s.v * s.v / (speed_goal * speed_goal)) * atan(BIG_SLIP / speed_goal);
- if (s.cur_rad < 0.0)
- bias = -bias;
-
- if(high_speed != 2)
- if (s.nex_rad == 0)
- speed_next = 250;
- else if (s.nex_rad < 0)
- speed_next = kurv_fart(WANTED_DIST - s.nex_rad);
- else
- speed_next = kurv_fart(s.nex_rad + WANTED_DIST);
-
- }
-
- alpha = STEER_GAIN * (s.to_lft - lane) / width - DAMP_GAIN * s.vn / s.v + bias;
-
- if (absolut(alpha) > 1.6)
- alpha = s.cur_rad < 0 ? -1.6 : 1.6;
-
- if (s.cur_rad == 0) {
- if (s.to_end < krit_avst(s.v, speed_next, RAK_BROMS)) {
- brake = 1;
- }
- } else if (s.to_end * (absolut(s.cur_rad) + lane) <= krit_avst(s.v, speed_next, KURV_BROMS)) {
- brake = 1;
- }
- if (s.after_rad != 0) {
- speed_after = kurv_fart(absolut(s.after_rad) + WANTED_DIST);
- if (s.cur_rad == 0) {
- if (s.to_end + s.nex_len * (absolut(s.nex_rad) + WANTED_DIST)
- <= krit_avst(s.v, speed_after, KURV_BROMS)) {
- brake = 1;
- }
- } else if (s.nex_rad == 0) {
- if (s.to_end * (absolut(s.cur_rad) + lane) + s.nex_len
- <= krit_avst(s.v, speed_after, KURV_BROMS)) {
- brake = 1;
- }
- } else {
- if (s.to_end * (absolut(s.cur_rad) + lane) + s.nex_len * (absolut(s.nex_rad) + WANTED_DIST)
- <= krit_avst(s.v, speed_after, KURV_BROMS)) {
- brake = 1;
- }
- }
- }
- if (brake)
- if (s.cur_rad == 0) {
- if (s.v > 1.02 * speed_next) {
- vc = s.v - RAK_BROMS_KONST;
- } else if (s.v < 0.98 * speed_next)
- vc = 1.1 * speed_next;
- else
- vc = 0.5 * (s.v + speed_next);
- } else {
- vc = s.v - kurv_kaning(s.cur_rad);
- }
- else if (s.cur_rad == 0)
- vc = s.v + 50;
- else
- vc = .5 * (s.v + speed_goal) / cos(alpha);
-
- if (s.dead_ahead)
- if (s.to_lft > s.to_rgt)
- lane -= DELTA_LANE;
- else
- lane += DELTA_LANE;
-
- // filut << "Alpha: " << alpha << " Vc: " << vc << " Radie: " << s.cur_rad <<" Vg: " << speed_goal;
- // filut << " Vn: " << speed_next<< " V: " << s.v << " Hs: " << high_speed << endl;
-
- result.vc = vc;
- result.alpha = alpha;
- return result;
- }
-
-